package jeconbond.experiment.ui.reportage;

import laboratoryQ.experiment.ui.UIUtils;
import laboratoryQ.experiment.ui.reportage.BaseER2CConverter;
import laboratoryQ.experiment.IExperimentResult;
import laboratoryQ.flatness.DiagramData;
import laboratoryQ.flatness.ui.DrawDiagramOnComponent;

import javax.swing.*;
import java.util.List;
import java.util.ArrayList;

import jeconbond.experiment.market.equilibrium.process.IMooreExperimentContext;
import jeconbond.experiment.market.equilibrium.MooreExperimentSettings;
import jeconbond.economic.market.agent.service.SpySellsAgent;
import jeconbond.economic.resources.IResourceType;
import jeconbond.experiment.computation.DiagramUtils;
import jeconbond.automata.behaviour.IAutomataWalkListener;

public class EconomicContextER2CConverter extends BaseER2CConverter {
	private static final String TIME = "Time";
	private static final String PRICES = "Prices";
	private static final String VALUES = "Values";
	private static final String ACTIVITY = "Activity";

	public EconomicContextER2CConverter(
			String experimentTitle
	) {
		super(
				experimentTitle,
				"Price",
				"Value",
				"Activity"
		);
		DrawDiagramOnComponent drawDiagramOnComponent = 
			(DrawDiagramOnComponent) diagramList2componentStrategy;
		drawDiagramOnComponent.setStylizer(
				UIUtils.configureDefaultUIStylizer()
		);
	}

	@Override
	public List<JComponent> convert2component(IExperimentResult expResult) {
		IMooreExperimentContext experimentContext =
			(IMooreExperimentContext) expResult;
		SpySellsAgent spySellsAgent = experimentContext.getSpySellsAgent();
		IAutomataWalkListener walkListener = experimentContext.getAutomataWalkListener();
		MooreExperimentSettings settings = experimentContext.getExperimentSettings();

		List<DiagramData> pricesExpView = new ArrayList<DiagramData>();
		List<DiagramData> valuesExpView = new ArrayList<DiagramData>();

		double[] timeStamps = DiagramUtils.generateTimeStamps(settings);

		IResourceType[] resources = settings.resources;
		for (int i = 0; i < resources.length; i++) {
			double[] prices = DiagramUtils.spyTrack2diagram(
					spySellsAgent.getType2priceTrack().get(resources[i])
			);
			DiagramData priceDia = new DiagramData(timeStamps, prices);
			DiagramUtils.replace_NAN_INF(priceDia.y, 0.0);
			pricesExpView.add(priceDia);

			double[] values = DiagramUtils.spyTrack2diagram(
					spySellsAgent.getType2valueTrack().get(resources[i])
			);
			DiagramData valuesDia = new DiagramData(timeStamps, values);
			DiagramUtils.replace_NAN_INF(valuesDia.y, 0.0);
			valuesExpView.add(valuesDia);
		}
		List<JComponent> resultList = new ArrayList<JComponent>();
		resultList.add(
				diagramList2componentStrategy.dia2component(pricesExpView, TIME, PRICES)
		);
		resultList.add(
				diagramList2componentStrategy.dia2component(valuesExpView, TIME, VALUES)
		);
		resultList.add(
				diagramList2componentStrategy.dia2component(
						createActivityDia(walkListener, timeStamps), 
						TIME, 
						ACTIVITY
				)
		);
		return resultList;
	}

	private static List<DiagramData> createActivityDia(
			IAutomataWalkListener walkListener,
			double[] timeStamps
	) {
		List<DiagramData> activityView = new ArrayList<DiagramData>();
		double[] prices = DiagramUtils.spyTrack2diagram(
				walkListener.getReport()
		);
		activityView.add(
				new DiagramData(timeStamps, prices)
		);
		return activityView;
	}
}
